package com.hzit.auth.controller;

import com.hzit.auth.service.GitEEService;
import com.hzit.common.entity.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Controller
@RequestMapping
public class GitEEController {
	@Autowired
	private GitEEService gitEEService;
	//1. gitee认证成功后回调地址
	@GetMapping("/gitee/callback/success")
	public String callbackSuccess(String code, HttpSession session){
		Map<String,String> resultMap = gitEEService.getUserInfo(code);
		System.out.println("resultMap = " + resultMap);
		// 1.1 将获取到的name放到共享的session
		String name = resultMap.get("name");
		String login = resultMap.get("login");
		String avatar_url = resultMap.get("avatar_url");
		// 1.2 将用户名放到session中
		LoginUser loginUser = new LoginUser(name,login,avatar_url);
		session.setAttribute("loginUser",loginUser);


		// 1.3 跳转回开始访问的页面,保存在session中的callback_url页面
		String callback_url = session.getAttribute("callback_url") + "";
		// 1.4 重新跳转到原来申请登录的页面
		return "redirect:" + callback_url;

	}
}
